package com.erli.exchange.common.sg;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import com.erli.exchange.common.sg.config.BaseClassInfo;
import com.erli.exchange.common.sg.entity.FieldInfo;

/**
 * 实体类生成器
 * 
 * @title EntitySourceGenerater.java
 * @package com.example.demo.sourcegenerate
 * @author lihu
 * @update 2019年3月8日 下午3:09:23
 */
public class EntitySourceGenerater extends AbstractSourceGenerator {

	private Map<String, List<FieldInfo>> fieldInfoListMap = new HashMap<String, List<FieldInfo>>();

	@Override
	protected String getFileMainContent() {
		List<FieldInfo> fieldList = this.getFieldList(this.tableNameThreadLocal.get());
		this.annotationSet.add(String.format(templateInfo.getEntityTableAnnotation(), this.tableNameThreadLocal.get()));
		StringBuilder content = new StringBuilder();
		String firstPropertyName = null;
		String firstColumnName = null;
		for (FieldInfo item : fieldList) {
			content.append(TAB_NEW_LINE);
			content.append(NOTE).append(item.getComment()).append(TAB_NEW_LINE);
			if (item.isPrimaryId()) {
				entityClassInfo.getIdAnnotationListInfo().getAnnotationList().forEach(annotation -> {
					content.append(annotation.getName()).append(TAB_NEW_LINE);
				});
				entityClassInfo.getIdImportListInfo().getImportList().forEach(importItem -> {
					this.importSet.add(importItem.getName());
				});
			} else {
				content.append(String.format(templateInfo.getEntityColumnAnnotation(), item.getColumnField()))
						.append(TAB_NEW_LINE);
			}

			this.importSet.addAll(this.getExtraImportByJavaType(content, item));
			content.append(String.format(templateInfo.getField(), item.getJavaType(), item.getPropertyName()))
					.append(TAB_NEW_LINE);
			if (firstPropertyName == null) {
				firstPropertyName = item.getPropertyName();
				firstColumnName = item.getColumnField();
			}
		}
		// 添加内部枚举类
		content.append(TAB_NEW_LINE);
		content.append(
				String.format(templateInfo.getEntityInnerEnum(), firstColumnName.toUpperCase(), firstPropertyName));
		return content.toString();
	}

	/**
	   * 获取额外的导入
	 * 
	 * @param content
	 * @param item
	 * @return
	 * @author lihu
	 * @update 2019年3月8日 下午3:18:17
	 */
	private List<String> getExtraImportByJavaType(StringBuilder content, FieldInfo item) {
		List<String> result = new ArrayList<>();
		if (item.getJavaType().equals("LocalDateTime")) {
			result.add("import java.time.LocalDateTime;");
		}
		return result;
	}

	@Override
	protected String getFileFirstLineContent() {
		String fileFirstLineTemplate = templateInfo.getFileStart();
		return String.format(fileFirstLineTemplate, baseInfo.getClassType(), this.getClassName(), "");
	}

	@Override
	protected BaseClassInfo getSelfClassInfo() {
		return entityClassInfo;
	}

	/**
	 * 根据表名获取Field
	 * 
	 * @param tableName
	 * @return
	 * @author lihu
	 * @update 2019年3月7日 下午6:32:11
	 */
	protected List<FieldInfo> getFieldList(String tableName) {
		if (fieldInfoListMap.containsKey(tableName)) {
			return fieldInfoListMap.get(tableName);
		}
		String databaseName = tablesInfo.getDatabase();
		DataSource dataSource = TableMetadataHelper.buildDataSource(datasourceInfo.getJdbcUrl(),
				datasourceInfo.getUser(), datasourceInfo.getPassword());
		List<FieldInfo> fieldInfoList = TableMetadataHelper.getFieldInfoByDataBaseNameAndTableName(dataSource,
				databaseName, tableName);
		fieldInfoListMap.put(tableName, fieldInfoList);
		return fieldInfoList;
	}
}
